package com.heitian.ssm.service.impl;

import com.heitian.ssm.dao.UserDao;
import com.heitian.ssm.model.PageBean;
import com.heitian.ssm.model.User;
import com.heitian.ssm.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

/**
 * 作者： 陈建峰
 * 创建时间： 17/10/16 下午4:13
 * 版本： [1.0, 17/10/16]
 * 描述： 用户处理
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    public PageBean<User> getAllUser(String userName,int currentPageIndex, int currentPageCount) {
        //封装一个PageBean 返回web层
        PageBean<User> pageBean = new PageBean<User>();
        //1、封装当前页
        pageBean.setCurrentPage(currentPageIndex);
        //2、封装每页显示的条数
        pageBean.setCurrentCount(currentPageCount);
        //3、封装总条数
        int totalCount = (int) userDao.getAllUserCount();
        pageBean.setTotalCount(totalCount);
        //4、封装总页数
        int totalPage = (int) Math.ceil(1.0*totalCount/currentPageCount);
        pageBean.setTotalPage(totalPage);
        //5、当前页显示的数据
        // select * from product where cid=? limit ?,?
        // 当前页与起始索引index的关系
        int index = (currentPageIndex-1)*currentPageCount;
        List<User> list = userDao.getAllUser(userName,index,currentPageCount);
        pageBean.setList(list);
        return pageBean;
    }

    public User getUserByUserGuid(String userGuid) {
        return userDao.getUserByUserGuid(userGuid);
    }

    public void deleteUser(String userGuid) {
        userDao.deleteUser(userGuid);
    }

    public void register(User newUser) {
        userDao.register(newUser.getUserGuid(),newUser.getUserName(),newUser.getPhotoUrl(),newUser.getSex(),newUser.getAdress(),
                newUser.getTelephoneNum(),newUser.getIsAdmin());
    }

}
